package com.changyi.device.printer;

import android.app.Activity;
import android.content.Context;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Handler;
import android.os.RemoteException;
import android.text.TextUtils;

import com.changyi.device.APApplication;
import com.changyi.device.TestXin;
import com.sunmi.peripheral.printer.InnerPrinterCallback;
import com.sunmi.peripheral.printer.InnerPrinterException;
import com.sunmi.peripheral.printer.InnerPrinterManager;
import com.sunmi.peripheral.printer.SunmiPrinterService;
import com.zqprintersdk.PrinterConst;
import com.zqprintersdk.ZQPrinterSDK;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xin on 2021/3/29
 * desc:
 */
public class T2Printer implements PrinterInterface {

    private Context mContext;
    private static T2Printer zqm2Printer;
    public static boolean isK1 = false;
    private int fontsizeTitle = 40;
    private int fontsizeContent = 30;
    private int fontsizeFoot = 35;
    private final Handler handler = new Handler();

    private T2Printer(Context context) {
        mContext = context;
    }

    public static T2Printer getInstance(Context context) {
        if (zqm2Printer == null) {
            synchronized (LandiA8Printer.class){
                if (zqm2Printer == null) {
                    zqm2Printer = new T2Printer(context);
                }
            }
        }
        return zqm2Printer;
    }


    @Override
    public PrinterInterface initDevice() {
//        isK1 = MyApplication.getInstance().isHaveCamera() && isVertical;
        if (isK1) {
//            connectKPrintService();
        } else {
            if (APApplication.printerService == null){
//                connectPrintService();
            }
        }

        return zqm2Printer;
    }

    @Override
    public PrinterInterface printTitle(final String title) {
        if (APApplication.printerService != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        APApplication.printerService.setAlignment(1, null);
                        APApplication.printerService.sendRAWData(boldOn(), null);
                        APApplication.printerService.printTextWithFont(title+ "\n","",fontsizeTitle,null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }else {
            APApplication.getInstance().connectPrintService();
        }


        return zqm2Printer;
    }

    @Override
    public PrinterInterface printText(final String text) {
        if (APApplication.printerService != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        APApplication.printerService.setAlignment(0, null);
                        APApplication.printerService.sendRAWData(boldOff(), null);
                        APApplication.printerService.printTextWithFont(text + "\n","",fontsizeContent,null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }else {
            APApplication.getInstance().connectPrintService();
        }
        return zqm2Printer;
    }

    @Override
    public PrinterInterface printList(final List<String> mList) {

        if (APApplication.printerService != null) {
            try {
                List<String> codes=new ArrayList<>();
                APApplication.printerService.setAlignment(0, null);
                APApplication.printerService.sendRAWData(boldOff(), null);
                for (String content : mList) {
                    if(content.contains("|")){
                        String[] aa = content.split("\\|");
                        for (int i = 0 ; i <aa.length ; i++ ) {
                            APApplication.printerService.printTextWithFont(content + "\n","",fontsizeContent,null);
                        }
                    }else  if (content.contains("@@@")){
                        codes.add(content);

                    } else  if (content.contains("&&&")){
                        codes.add(content);
                    } else if(content.equals("  ")){
                        codes.add(content);
                    } else{
                        APApplication.printerService.printTextWithFont(content + "\n","",fontsizeContent,null);
                    }

                }

                APApplication.printerService.sendRAWData(boldOff(), null);
                APApplication.printerService.lineWrap(3, null);
                APApplication.printerService.cutPaper(null);

                if(codes!=null&&codes.size()>0) {
//                             printBitmaps(codes);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
            APApplication.getInstance().connectPrintService();
        }




        return zqm2Printer;
    }


    @Override
    public PrinterInterface printCode(String code) {
        return zqm2Printer;
    }

    @Override
    public PrinterInterface printQrCode(String qRCode) {
        return zqm2Printer;
    }

    @Override
    public PrinterInterface feedLine(int feelLine) {
        if (APApplication.printerService != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        APApplication.printerService.sendRAWData(boldOff(), null);
                        APApplication.printerService.lineWrap(3, null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }else {
            APApplication.getInstance().connectPrintService();
        }


        return zqm2Printer;
    }

    public PrinterInterface printBitmaps(List<String> codes) {
        initDevice();
        for (String content : codes) {
            if (content.contains("@@@")) {
                String temp = content.substring(3, content.length());
//                sdk.setPrnBitmap(ZXingUtils.encodeAsBitmap(temp, BarcodeFormat.QR_CODE, 200, 200), 1);

            } else if (content.contains("&&&")) {
                String temp = content.substring(3, content.length());
//                sdk.setPrnBitmap(ZXingUtils.creatBarcode(mContext, temp, 300, 60, false), 0);
            } else{
//                sdk.setPrnText(content.trim()+"\n", 1, 0);
            }
        }
//        sdk.feedPaper(10,0);
//        sdk.startPrint();
        return zqm2Printer;
    }


    private static final byte ESC = 0x1B;// Escape
    /**
     * 字体加粗
     */
    private byte[] boldOn() {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 69;
        result[2] = 0xF;
        return result;
    }

    /**
     * 取消字体加粗
     */
    private byte[] boldOff() {
        byte[] result = new byte[3];
        result[0] = ESC;
        result[1] = 69;
        result[2] = 0;
        return result;
    }

}
